﻿using DbFactory;
using PlaneClean.Models;
using PlaneClean.ViewModels;
using PlaneClean.ViewModels.User;
using PlaneCleanEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace PlaneClean.Controllers.ManagerController
{
    /// <summary>
    /// 用户角色管理
    /// </summary>
    public class UserGroupController : ManagerController
    {

        /// <summary>
        /// 根据角色查询用户列表
        /// </summary>
        /// <param name="groupId"></param>
        /// <returns></returns>
        [HttpPost]
        public ListResponse<t_user> List(int groupId)
        {
            ListResponse<t_user> response = new ListResponse<t_user>();
            response.bodyList = new List<t_user>();
            response.result = "NG";
            List<int> uerIdList = dbFactory.Db.Queryable<t_user_group>().Where(a => a.g_id == groupId).Select(a => a.u_id).ToList();
            List<t_user> uList = dbFactory.Db.Queryable<t_user>().Where(a => uerIdList.Contains(a.u_id)).ToList();
            response.bodyList.AddRange(uList);

            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 新增角色用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResponse Add(BaseRequest<t_user_group> request)
        {
            BaseResponse response = new BaseResponse();
            t_user_group ug = DbFactory.dbFactory.Db.Queryable<t_user_group>().Where(a => a.g_id == request.body.g_id && a.u_id == request.body.u_id).First();
            if (ug != null)
            {
                response.reason = "该角色已存在该用户,新增失败";
                return response;
            }
            int pId = DbFactory.dbFactory.Db.Insertable(request.body).IgnoreColumns(ignoreNullColumn: true).ExecuteReturnIdentity();
            if (pId <= 0)
            {
                response.reason = "插入数据库异常,新增失败";
                return response;
            }
            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResponse Delete(DeleteRequest request)
        {
            BaseResponse response = new BaseResponse();
            //response.reason = request.idList.Count.ToString();

            if (request.groupId <= 0)
            {
                response.reason = "请传入需要删除用户角色的主键";
                return response;
            }
            if (request.idList == null || request.idList.Count <= 0)
            {
                response.reason = "请传入需要删除角色用户的主键";
                return response;
            }
            dbFactory.Db.Deleteable<t_user_group>().Where(a => a.g_id == request.groupId && request.idList.Contains(a.u_id)).ExecuteCommand();
            response.result = "OK";
            return response;
        }
    }
}